package cn.school.service.impl;


import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.school.dto.CancelAppointDto;
import cn.school.dto.ExamAppointDto;
import cn.school.pojo.Students;
import cn.school.service.StudentsService;
import cn.school.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.school.pojo.ExamAppoint;
import cn.school.service.ExamAppointService;
import cn.school.mapper.ExamAppointMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* @author Lenovo
* @description 针对表【exam_appoint】的数据库操作Service实现
* @createDate 2025-06-16 20:43:04
*/
@Service
public class ExamAppointServiceImpl extends ServiceImpl<ExamAppointMapper, ExamAppoint>
    implements ExamAppointService{
    @Resource
    private StudentsService studentsService;
    @Override
    public ResultVo appoint(ExamAppointDto examAppointDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Students> studentsQueryWrapper=new QueryWrapper<>();
        studentsQueryWrapper.eq("user_id",id);
        Students students = studentsService.getOne(studentsQueryWrapper);
        if (students==null){
            return ResultVo.error("您当前不是学生预约失败");
        }else {
            ExamAppoint examAppoint=new ExamAppoint();
            examAppoint.setExamStatus(0);
            examAppoint.setPaperExamId(Long.valueOf(examAppointDto.getExamId()));
            examAppoint.setSessionId(Integer.valueOf(examAppointDto.getSessionId()));
            examAppoint.setStudentsId(students.getId());
            this.save(examAppoint);
            return ResultVo.ok("预约成功");
        }
    }

    @Override
    public ResultVo cancelAppoint(CancelAppointDto cancelExamAppointDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Students> studentsQueryWrapper=new QueryWrapper<>();
        studentsQueryWrapper.eq("user_id",id);
        Students students = studentsService.getOne(studentsQueryWrapper);
        Long id1 = students.getId();
        QueryWrapper<ExamAppoint> examAppointQueryWrapper=new QueryWrapper<>();
        examAppointQueryWrapper.eq("students_id",id1);
        examAppointQueryWrapper.eq("paper_exam_id",cancelExamAppointDto.getExamId());
        examAppointQueryWrapper.eq("session_id",cancelExamAppointDto.getSessionId());
        ExamAppoint examAppoint = this.getOne(examAppointQueryWrapper);
        Integer examStatus = examAppoint.getExamStatus();
        if (examStatus==0){
            examAppoint.setExamStatus(4);
            this.updateById(examAppoint);
            return ResultVo.ok("取消预约成功");
        }else {
            return ResultVo.error("不在取消预约范围内");
        }
    }
}




